package org.daidai.abstrusewriting.homepage;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import org.daidai.abstrusewriting.R;

import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BroadCastActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static Queue<String> hrefQueue;
    private static Queue<String> cashQueue;
    private Context context=this;
    private MediaPlayer player0;
    private MediaPlayer player1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad_cast);

        hrefQueue = new LinkedList<>();
        cashQueue=new LinkedList<>();


        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean isPalying=false;
                while (true){
                    getM3u8();
                    if (!isPalying) {
                        player0=updatePlayer(player0);
                        play();
                        isPalying=true;
                    }
                    try {
                        Thread.sleep(7000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }


            }
        }).start();

    }


    public void getM3u8(){
        byte[] bytes = HttpUtils.doGet("http://live.xmcdn.com/live/93/24.m3u8");
        try {

            String str = new String(bytes, "UTF-8");
            //Log.d(TAG, "onCreate: "+str);
            findHref(str,hrefQueue);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public void play(){

        player1 = updatePlayer(player1);
        player0.start();
        player0.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {

                player1.start();
                mp.reset();
                mp.release();
                player0=updatePlayer(player0);
                player1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {

                        play();
                        mp.reset();
                        mp.release();
                    }
                });

            }
        });
    }


    public MediaPlayer updatePlayer(MediaPlayer player){


        String href = hrefQueue.poll();
        Uri uri = Uri.parse(href);
        player=MediaPlayer.create(this,uri);

        return player;
    }
    public static void findHref(String str,Queue<String> queue){
        Pattern p=Pattern.compile("http://.{1,}\\.aac");
        Matcher matcher = p.matcher(str);

        while (matcher.find()){
            String href = matcher.group(0);

            boolean flag=false;
            if (!cashQueue.contains(href)) {
                queue.add(href);
                Log.d(TAG, "findHref: "+ href);
                if(cashQueue.size()<10){
                    cashQueue.add(href);
                }else{
                    cashQueue.add(href);
                    cashQueue.poll();
                }
            }

        }
    }


    @Override
    protected void onDestroy() {
        player0.reset();
        player0.release();
        player0=null;

        player1.reset();
        player1.release();
        player1=null;
        super.onDestroy();
    }
}
